home *** CD-ROM | disk | FTP | other *** search
- /* ps-pict2macbin.c george jefferson george@mech.seas.upenn.edu */
- /* initial release 4/4/92 */
- /* usage ps-pict2macbin file [-type TYPE ] [ -creator CREATOR ] */
- /* combines "file.ps" and "file.pict" to form "file.bin" */
- /* file.bin is a macbinary format file, with macintosh name "file" */
- /* and creation/modification dates set to the current time of */
- /* day ( anybody care? ) */
- /* if type and creator are not specified, they are set to EPSF and MSWD */
- /* because _I_ use word5 */
- /* I see no reason to provide options to change the file names, */
- /* but it could easily be done */
- /* "file.ps" is taken to be the data fork, no assumptions are made here */
- /* about its format, however it is presumably postscript code -- It seems */
- /* to be necessary to convert line endings to macintosh endings ( ^M ) */
- /* I think it more appropriate to do that conversion externally ( eg tr ) */
- /* */
- /* "file.pict" is a pict data file, which is massaged into the resource */
- /* fork Its format _is_ important here. */
- /* ppmtopict output is assumed, which ( for no apparent reason ) contains */
- /* lots of null bytes prior to the actual data.. */
- /* ( in other words this program will not work with other types of */
- /* resources ) */
- /* the format of the pict resource fork can be found in IM 1, page 128-131*/
- /* many assumptions are 'hard wired' into this program -- eg we have */
- /* exactly one resource, of type PICT */
- /* */
- /* It will be painfully obvious that this is the work of a novice C */
- /* programmer, in fact the program is originally done in FORTRAN and (hand)*/
- /* converted ( note the lack of struct's and such ) */
- /* anyway, it works on several sun4-ish machines around here, your milage */
- /* may vary */
-
-
- #include <stdio.h>
- #include <sys/types.h>
- #ifdef notimeb
- #else
- #include <sys/timeb.h>
- #endif
-
-
- main (argc,argv)
- int argc; char **argv;
- {
- int i,j,ksize;
- unsigned long mtim=0,ctim=0,unixtime,sizef,realsizef;
- int ireslength,idatalength,ipad;
- FILE *pic,*bin, *ps;
- char outname[30],inname[30],word[4],byte[1],zero[1];
- char ftyp[4];
- char fcrea[4];
- char macname[63], basename[30], psname[30], bbuf[128];
- extern unsigned long time2mac();
- if( argc <= 1 )
- {
- fprintf(stderr,
- "usage ps-pict2macbin.c file.ps [ -type TYPE ][-creator CRTR ]\n");
- exit();
- }
- strcpy(basename,argv[1]);
- strcpy(inname,basename);strcat(inname,".pict");
- strcpy(outname,basename);strcat(outname,".bin");
- bzero(macname,63);
- strcpy(macname,basename);
- strcpy(psname,basename);strcat(psname,".ps");
- fprintf(stderr,"opening pict file %s \n",inname);
- if( (pic=fopen(inname,"r")) == NULL )
- {fprintf(stderr,"error opening file\n");exit();}
- fprintf(stderr,"opening output file %s \n",outname);
- if( (bin=fopen(outname,"w")) == NULL )
- {fprintf(stderr,"error opening file\n");exit();}
- fprintf(stderr,"opening ps file %s \n",psname);
- if( (ps=fopen(psname,"r")) == NULL )
- {fprintf(stderr,"error opening file\n");exit();}
- for(i=1;i<=128;i++)write_one(bin,0); /*leave room for info */
- /* copy ps file over to binary */
- idatalength=0;i=0;
- while (fread(byte, sizeof(*byte), 1, ps) > 0)
- {
- if(++i==129)i=1;
- ++idatalength;
- fwrite(byte, sizeof(*bbuf), 1, bin);
- }
- fclose(ps);
- for(i=i;i<128;i++)write_one(bin,0); /*pad to next 128 byte boundary */
- /* write the pict resource from the pict data file */
- fread(byte,sizeof(*byte),1,pic);i=1;
- while( strlen(byte)==0 ){fread(byte,sizeof(*byte),1,pic);i++;}
- ksize=256*ibyte(byte);
- fread(byte,sizeof(*byte),1,pic);
- ksize=ksize+ibyte(byte); /* length of actual resource data */
- sizef=ftell(pic);fseek(pic,0l,2);realsizef=ftell(pic)-i+1;fseek(pic,sizef,0);
- if(ksize != realsizef)fprintf(stderr,"real length %i not reported length %i fixing...\n",realsizef,ksize);
- ireslength=256+realsizef+4+50; /* total length of resource fork */
- write_header(bin,realsizef);
- for(i=1;i<=112;i++)write_one(bin,0);
- for(i=1;i<=128;i++)write_one(bin,0); /*reserver stuff */
- write_four(bin,realsizef); /* length of resource data */
- write_two(bin,ksize); /* length of resource data */
- for( j=1;j<=realsizef-2;j++){
- fread(byte,sizeof(*byte),1,pic);
- fwrite(byte,sizeof(*byte),1,bin);
- }
- /* resource map */
- write_header(bin,realsizef);/* copy of header */
- write_four(bin,0);write_two(bin,0); /* 6 null bytes ( reserved ) */
- write_two(bin,0); /* attributes*/
- write_two(bin,28); /* offset from beginning of map to type list */
- write_two(bin,28+22); /* beg map to name list */
- /* type list */
- write_two(bin,0); /* only one resource type */
- strcpy(word,"PICT");
- fwrite(word,sizeof(word),1,bin);/* resource type */
- write_two(bin,0); /* only one pict */
- write_two(bin,10); /* begining of type list to reference list */
- /* reference list */
- write_two(bin,256); /* resource id */
- write_one(bin,-1);write_one(bin,-1); /* no name for resource */
- write_one(bin,0); /* attributes */
- write_two(bin,0);write_one(bin,0) ; /* 3 byte offset within resourc */
- write_four(bin,0); /* reserved */
- /* pad file to nearest 128 byte boundary */
- ipad=(ireslength/128+1)*128-ireslength;
- for(i=1;i<=ipad;i++)write_one(bin,0);
- /* this is the end of the resource fork, and the end of the file */
- /* macbinary info header */
- fseek(bin,0l,0);/* rewind so that we can write the info header */
- write_one(bin,0); /* zero */
- write_one(bin,strlen(macname)); /* length of file name */
- fwrite(macname,sizeof(macname),1,bin); /* name padded to 63 bytes */
- bcopy("MSWD",fcrea,4);
- bcopy("EPSF",ftyp,4);
- for( i=1;i<argc;i++ )
- {
- if( strcmp(argv[i],"-type") == 0 )bcopy(argv[++i],ftyp,4);
- if( strcmp(argv[i],"-creator") == 0 )bcopy(argv[++i],fcrea,4);
- }
- fwrite(ftyp,sizeof(ftyp),1,bin);/* file type */
- fwrite(fcrea,sizeof(fcrea),1,bin);/* file creator */
- write_one(bin,1); /* should this be a zero? */
- write_one(bin,0);
- write_four(bin,0);write_two(bin,0);
- write_one(bin,0);
- write_one(bin,0);
- write_four(bin,idatalength); /* length og data file */
- write_four(bin,ireslength); /* length of resource file */
- unixtime = time(&unixtime);
- ctim=time2mac(unixtime);
- write_four(bin,ctim);
- write_four(bin,mtim);
- /* all done */
- }
-
- write_header(iu,kbytes)
- FILE *iu;int kbytes;
- {
- write_four(iu,256);/*c 4 bytes -> length of header+system+application, always 16+112+128=256*/
- /*c next four bytes -> length of pict data + header + 4 bytes for size*/
- write_four(iu,256+kbytes+4);
- /*c next four -> length of data*/
- write_four(iu,kbytes+4);
- /*c next four -> length of map ( always same )*/
- write_four(iu,50);
- }
-
- write_one(iu,k)
- int k ; FILE *iu ;
- {
- unsigned char byte[1];
- byte[0]=k;
- fwrite(byte,sizeof(*byte),1,iu);
- }
-
-
-
-
- ibyte( b )
- char *b;
- {
- int ibyte;
- ibyte=b[0];
- if(ibyte < 0 )ibyte=ibyte+256;
- return(ibyte);
- }
-
-
- write_four(iu,k)
- unsigned long k ; FILE *iu ;
- {
- int k1,k2,l,n[4];
- unsigned char byte[1];
- n[0]=16777216;n[1]=65536;n[2]=256;n[3]=1;
- k1=0;
- for(l=0;l<4;l++)
- {
- k2=(k-k1)/n[l];
- byte[0]=k2;
- fwrite(byte,sizeof(*byte),1,iu);
- k1=k1+k2*n[l];
- }
- }
-
- write_two(iu,k)
- int k ; FILE *iu ;
- {
- int k1,k2,l,n[2];
- unsigned char byte[1];
- n[0]=256;n[1]=1;
- k1=0;
- for(l=0;l<2;l++)
- {
- k2=(k-k1)/n[l];
- byte[0]=k2;
- fwrite(byte,sizeof(*byte),1,iu);
- k1=k1+k2*n[l];
- }
- }
-
-
- #ifndef sysv
- typedef unsigned long ulong; /* 4 bytes */
- #endif
-
-
- /* 'borrowed' from mcvert */
- /* Convert Unix time (GMT since 1-1-1970) to Mac
- time (local since 1-1-1904) */
- #define MACTIMEDIFF 0x7c25b080 /* Mac time of 00:00:00 GMT, Jan 1, 1970 */
-
- /* Compatibility issues */
- #ifdef BSD
- #define long2mac (ulong) htonl
- #else
- #define long2mac
- #endif
-
- ulong time2mac(time)
- ulong time;
- {
- #ifdef notimeb
- return long2mac( time + MACTIMEDIFF );
- #else
- struct timeb tp;
- ftime(&tp);
- return long2mac(time + MACTIMEDIFF
- - 60 * (tp.timezone /*- 60 * tp.dstflag*/));
- #endif
- }
- /* correcting for daylight savings time needs to be improved. */
-